package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.adapter.MyWangGuanAdapter;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.WangGuanDeviceEntity;
import com.argrace.smart.ui.EmptyRecycleView;
import com.argrace.smart.util.AlinkHttpUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/2/11.
 */

public class MineGwDeviceListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    EmptyRecycleView recyclerView;
    SwipeRefreshLayout refreshLayout;
    private MyWangGuanAdapter adapter;
    private ArrayList<WangGuanDeviceEntity> lists = new ArrayList<>();
    private int act_type = 0;
    private String uuid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_minegwdevicelist);
        init();
    }

    private void init() {
        act_type = getIntent().getIntExtra("type", ConstantConfigure.DEFAULT_ZERO);
        setView();
        setRefreshing(true);
        onRefresh();
    }

    private void setView() {
        recyclerView = (EmptyRecycleView) findViewById(R.id.recyclerView);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyWangGuanAdapter(lists, this);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
//        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme));//getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme)
        refreshLayout.setOnRefreshListener(this);
        adapter.setListeners(new MyWangGuanAdapter.OnAdapterItemClickListener() {
            @Override
            public void click(WangGuanDeviceEntity bean, int position) {
                //如果是选择设备，点击后进入添加情景界面
                if (act_type == ConstantConfigure.ACT_TYPE_CHOOSEDEVICE) {
                    Intent intent = new Intent(MineGwDeviceListActivity.this, CreateQingJingActivity.class);
                    intent.putExtra("uuid", bean.getUuid());
                    startActivity(intent);
                }
            }
        });
        adapter.setlongListeners(new MyWangGuanAdapter.OnAdapterItemLongClickListener() {
            @Override
            public void click(WangGuanDeviceEntity bean, int position) {
            }
        });
    }

    @Override
    public void onRefresh() {
        getMyDevice();
    }

    @Override
    public void onLoadMoreRequested() {
        getMyDevice();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    private void getMyDevice() {
        Log.i("info", "MainActivity.getMyDevice()start");
        String method = "mtop.openalink.app.core.devices.getbyuser";
        AlinkHttpUtil.AlinkHttpRequest(method, new HashMap<String, Object>(), new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                lists.clear();
                setRefreshing(false);
                adapter.loadMoreEnd();
                if (transitoryResponse != null && transitoryResponse.data != null) {
                    Log.i("info", "MainActivity.getMyDevice()--success,data=" + transitoryResponse.data.toString());
                    List<WangGuanDeviceEntity> beans = JSON.parseArray((String) transitoryResponse.data, WangGuanDeviceEntity.class);
                    lists.addAll(beans);
                }
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                setRefreshing(false);
                adapter.loadMoreEnd();
                Log.i("info", "MainActivity.getMyDevice()--onFailed,code = " + aError.getCode() + ",msg=" + aError.getMsg());
            }
        });
    }

}
